//
//  LoginAndRegister.m
//  Comma
//
//  Created by 刘轩博 on 15/1/17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LoginAndRegister.h"

@implementation LoginAndRegister

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (instancetype)initWithLeftUpButton:(NSString *)leftUpButton 
                       rightUpButton:(NSString *)rightUpButton upTextField:(NSString *)upTextField
                     centerTextField:(NSString *)centerTextField
                       downTextField:(NSString *)downTextField 
                            upButton:(NSString *)upButton 
                        centerButton:(NSString *)centerButton
                          downButton:(NSString *)downButton
                    centerDownButton:(NSString *)centerDownButton
{
    self = [super initWithFrame:CGRectMake(0, 312, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        if (leftUpButton) {
            [self createLeftUpButtonWithTitle:leftUpButton];
        }
        if (rightUpButton) {
            [self createRightUpButtonWithTitle:rightUpButton];
        }
        if (upTextField) {
            [self createUpTextFieldWithText:upTextField];
        }
        if (centerTextField) {
            [self createCenterTextFieldWithText:centerTextField];
        }
        if (downTextField) {
            [self createDownTextFieldWithText:downTextField];
        }
        if (upButton) {
            [self createUpButtonWithTitle:upButton];
        }
        if (centerButton) {
            [self createCenterButtonWithTitle:centerButton];
        }
        if (downButton) {
            [self createDownButtonWithTitle:downButton];
        }
        if (centerDownButton) {
            [self createCenterDownButtonWithTitle:centerDownButton];
        }
        [self registerForKeyboardNotifications];
        self.viewDidChange = NO;
        
    }
    return self;
}

#pragma mark - 创建leftUpButton
- (void)createLeftUpButtonWithTitle:(NSString *)title
{
    self.leftUpButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _leftUpButton.frame = CGRectMake(16 + SCREEN_WIDTH, 0, 58, 44);
    [_leftUpButton setTitle:title forState:UIControlStateNormal];
    [_leftUpButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_leftUpButton.titleLabel setFont:BOLDENGLISH_FONT(14.0)];
    [self addSubview:_leftUpButton];
}

#pragma mark - 创建rightUpButton
- (void)createRightUpButtonWithTitle:(NSString *)title
{
    self.rightUpButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _rightUpButton.frame = CGRectMake(SCREEN_WIDTH - 57 + SCREEN_WIDTH, 0, 41, 44);
    [_rightUpButton setTitle:title forState:UIControlStateNormal];
    [_rightUpButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_rightUpButton.titleLabel setFont:BOLDENGLISH_FONT(14.0)];
    [self addSubview:_rightUpButton];
}

#pragma mark - 创建upTextField
- (void)createUpTextFieldWithText:(NSString *)text
{
    self.upTextField = [[UITextField alloc] initWithFrame:CGRectMake(16 + SCREEN_WIDTH, 44, SCREEN_WIDTH - 16, 44)];
    _upTextField.delegate = self;
    _upTextField.placeholder = text;
    [_upTextField setTextColor:TEXTERGRAY];
    [_upTextField setFont:BOLDENGLISH_FONT(14.0)];
    [self addSubview:_upTextField];
}

#pragma mark - 创建centerTextField
- (void)createCenterTextFieldWithText:(NSString *)text
{
    self.centerTextField = [[UITextField alloc] initWithFrame:CGRectMake(16 + SCREEN_WIDTH, 88, SCREEN_WIDTH - 16, 44)];
    _centerTextField.delegate = self;
    _centerTextField.placeholder = text;
    [_centerTextField setTextColor:TEXTERGRAY];
    [_centerTextField setFont:BOLDENGLISH_FONT(14.0)];
    [self addSubview:_centerTextField];
}

#pragma mark - 创建downTextField
- (void)createDownTextFieldWithText:(NSString *)text
{
    self.downTextField = [[UITextField alloc] initWithFrame:CGRectMake(16 + SCREEN_WIDTH, 132, SCREEN_WIDTH - 16, 44)];
    _downTextField.delegate = self;
    _downTextField.placeholder = text;
    [_downTextField setTextColor:TEXTERGRAY];
    [_downTextField setFont:BOLDENGLISH_FONT(14.0)];
    [self addSubview:_downTextField];
}

#pragma mark - 创建upButton
- (void)createUpButtonWithTitle:(NSString *)title
{
    self.upButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_upButton setTitle:title forState:UIControlStateNormal];
    [_upButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_upButton.titleLabel setFont:BOLDENGLISH_FONT(14.0)];
    [self addSubview:_upButton];
}

#pragma mark - 创建centerButton
- (void)createCenterButtonWithTitle:(NSString *)title
{
    self.centerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_centerButton setTitle:title forState:UIControlStateNormal];
    [_centerButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_centerButton.titleLabel setFont:ENGLISH_FONT(14.0)];
    [self addSubview:_centerButton];
}

#pragma mark - 创建downButton
- (void)createDownButtonWithTitle:(NSString *)title
{
    self.downButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_downButton setTitle:title forState:UIControlStateNormal];
    [_downButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_downButton.titleLabel setFont:BOLDENGLISH_FONT(14.0)];
    [self addSubview:_downButton];
}

#pragma mark - 创建centerDownButton
- (void)createCenterDownButtonWithTitle:(NSString *)title
{
    self.centerDownButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_centerDownButton setFrame:CGRectMake(SCREEN_WIDTH / 2 - 50 + SCREEN_WIDTH, 204, 100, 44)];
    [_centerDownButton setTitle:title forState:UIControlStateNormal];
    [_centerDownButton.titleLabel setFont:BOLDENGLISH_FONT(16.0)];
    [_centerDownButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [self addSubview:_centerDownButton];
    
}


#pragma mark - 创建register选择按钮
- (void)createRegisterButton
{
    self.registButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _registButton.frame = CGRectMake(158.5, 44, 58, 44);
    [_registButton setTitle:@"注 册" forState:UIControlStateNormal];
    [_registButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_registButton.titleLabel setFont:BOLDENGLISH_FONT(14.0)];
    _registButton.tag = 1;
    [self addSubview:_registButton];
    
}

#pragma mark - 创建login选择按钮
- (void)createLoginButton
{
    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _loginButton.frame = CGRectMake(158.5, 88, 58, 44);
    [_loginButton setTitle:@"登 录" forState:UIControlStateNormal];
    [_loginButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_loginButton.titleLabel setFont:BOLDENGLISH_FONT(14.0)];
    _loginButton.tag = 2;
    [self addSubview:_loginButton];
}

#pragma mark - 由键盘更改View高度
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification
{
    
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.keyBoardHeight = kbSize.height;
    self.keyBoardDidShow = YES;
    [self viewWillChangeByKeyBoard];
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.editingTextField = nil;
    self.editingTextField = textField;
    if (_keyBoardDidShow) {
        [self viewWillChangeByKeyBoard];
    }
}

- (void)viewWillChangeByKeyBoard
{
    CGFloat differ = _editingTextField.frame.origin.y + _editingTextField.frame.size.height + 312 - SCREEN_HEIGHT + _keyBoardHeight;
    if (_viewDidChange) {
        if (differ < 0) {
            [UIView animateWithDuration:0.1 animations:^{
                [self.superview setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            } completion:^(BOOL finished) {
                self.viewDidChange = NO;
            }];
        }
    }
    else {
        if (differ > 0) {
            [UIView animateWithDuration:0.1 animations:^{
                [self.superview setFrame:CGRectMake(0, - differ, SCREEN_WIDTH, SCREEN_HEIGHT)];
            } completion:^(BOOL finished) {
                self.viewDidChange = YES;
            }];
        }
    }
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
    if (_viewDidChange) {
        [UIView animateWithDuration:0.1 animations:^{
            [self.superview setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        } completion:^(BOOL finished) {
            self.viewDidChange = NO;
        }];
    }
    self.keyBoardDidShow = NO;
}
#pragma mark - 根据键盘改变视图的高度 -end

/*画线*/
+ (void)drawLineWithStartPoint:(CGPoint)startPoint size:(CGSize)size inView:(UIView *)inView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(startPoint.x, startPoint.y, size.width, size.height)];
    view.backgroundColor = DIVIDINGLINECOLOR;
    [inView addSubview:view];
}



@end
